#!/usr/bin/python3
#引入必要文件
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException,UnexpectedAlertPresentException,NoAlertPresentException
from selenium.webdriver.support.select import Select
import random
import time
#初始化浏览器驱动
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(options=options)
#初始化部分值
learn=[]
remark = 0
name = "未登录"
StuNum="0"
login=0
random.seed(114514)
#随机布尔函数，输出0和1
def RBool():
    get=random.random()
    if get>0.5:
        return 1;
    else:
        return 0;
#登录系统到主界面
def Login():
    global StuNum
    remark=0
    StuNum=input("请输入你的学号：")
    StuPass=input("请输入你的密码：")
    #登录系统
    driver.find_element(By.NAME,'txtUserName').send_keys(StuNum)
    #判断登录
    flag1=1
    while(flag1):
        flag2=1
        driver.find_element(By.NAME,"TextBox2").send_keys(StuPass)
        #后续更新为OCR识别
        #出错捕获，比如验证码错误，密码错误
        while(flag2):
            get = input("请您在这输入验证码,输入-1重新获取：")
            if get=='-1':
                driver.execute_script("reloadcode()")
            else:
                flag2=0
        driver.find_element(By.NAME,'txtSecretCode').send_keys(get)
        driver.find_element(By.NAME,'Button1').click()
        #判断是否成功登录
        try:
            driver.switch_to.alert
        except NoAlertPresentException:
            flag1=0
        else:
            driver.switch_to.alert.accept()
            print("验证码不正确")
    global name 
    name = driver.find_element(By.XPATH,"/html/body/div/div[1]/div[1]/form/div/ul/li[1]/em/span").text
    global login 
    login = 1    
#进入教学质量评价界面
def beforeremark():
    #获取可评价的课程名字
    for clas in driver.find_elements(By.XPATH, "//*[@id=\"headDiv\"]/ul/li[4]/ul/li"):        
        clast = clas.find_element(By.TAG_NAME,'a').accessible_name
        learn.append(clast)
    #判断是否能够评价
    if(len(learn)==0):
        input("目前无需评价，任意键回车返回")
        return
    print('你当前有'+str(len(learn))+"门课需要进行评价")
    input
    a = input("输入-1可以查看你可以评价了什么课，只能输入数字：")
    if int(a) == -1:
        for i in learn:
            print(i)
        input("输入任意键继续")
    driver.find_element(By.XPATH, "//*[@id=\"headDiv\"]/ul/li[4]").click()
    driver.find_element(By.XPATH, "//*[@id=\"headDiv\"]/ul/li[4]/ul/li[1]").click()
    driver.switch_to.frame("zhuti")
    i=1
    time.sleep(1)
    try:
        driver.switch_to.alert
    except NoAlertPresentException:
        flag1=1
    else:
        driver.switch_to.alert.accept()
        print("已经评价完，无需再评价")
        global remark
        remark=1
    time.sleep(1)
    while(remark==0):
        while(i<len(learn)+1):
            print("正在填写 "+learn[i-1]+" 中")
            Remark()
            time.sleep(2)
            i=i+1
        time.sleep(2)
        try:
            driver.switch_to.alert.accept()
        except NoAlertPresentException:
            print("已验证提示框。")
            remark=1
        
    a=input("评测完成，如果您认为有些需要修改，请自行在浏览器修改，\n并在修改完成后在终端输入任意键回车。")
    driver.find_element(By.XPATH, "/html/body/form/div[2]/div/div[2]/em/span[2]/input[6]").click()
    time.sleep(2)
    try:
        driver.switch_to.alert
    except NoAlertPresentException:
        flag1=1
    else:
        driver.switch_to.alert.accept()
        input("教学质量评价完成，按任意键后回车键下一步")
#评价过程
def Remark():
    #判断数量
    time.sleep(1)
    num =driver.find_element(By.XPATH,("/html/body/form/div[2]/div/span/table/tbody/tr[3]/td/fieldset/div/table/tbody/tr[2]/td[2]")).accessible_name
    num = int(num)
    if num==0:
        num=15
    else:
        num=17
    i=2
    while i<num:
        remarkbox = driver.find_element(By.NAME,"DataGrid1:_ctl"+str(i)+":JS1")
        select_remarkbox = Select(remarkbox)
        select_remarkbox.select_by_index(RBool()+1)
        i=i+1
    driver.find_element(By.XPATH,"//*[@id=\"Button1\"]").click()
    return

def About():
    print("基于Python")


#主函数
driver.get("https://jws.syuct.edu.cn/")
while(True):
    print(8*"-"+"主界面"+8*"-")
    print("欢迎使用沈阳化工大学教务系统辅助工具")
    if(login):
        print("学号：%s\n姓名：%s"%(StuNum,name))
    else:
        print("当前未登录")
    print("Version 1 20240620")
    print(8*"-"+"选项"+8*"-")
    if(login==0):
        print("1）登录系统  2）关于")
        choice=input("请输入数字选项")
        match choice:
            case '1':
                Login()
            case '2':
                About()
            case _:
                print("你的输入有误")

    else:
        print("1）教学质量评价 2）关于")
        choice=input("请输入数字选项")
        match choice:
            case '1':
                beforeremark()
            case '2':
                About()
            case _:
                print("你的输入有误")

    



